package com.dingreading.cloud.admin.service.impl;

import com.dingreading.cloud.admin.entity.CmactivationCode;
import com.dingreading.cloud.admin.entity.CmactivationCodeLog;
import com.dingreading.cloud.admin.entity.CmactivationCodeType;
import com.dingreading.cloud.admin.entity.Stores;
import com.dingreading.cloud.admin.service.*;
import com.dingreading.cloud.admin.util.AdminEntityUtils;
import com.dingreading.cloud.common.base.R;
import com.dingreading.cloud.common.util.PageDto;
import com.dingreading.cloud.common.util.PageUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public class CmactivationCodeCommonServiceImpl implements CmactivationCodeCommonService{
    @Resource
    private StoresService storesService;
    @Resource
    private CmactivationCodeLogService cmactivationCodeLogService;
    @Resource
    private CmactivationCodeService cmactivationCodeService;
    @Resource
    private CmactivationCodeTypeService cmactivationCodeTypeService;

    @Override
    public R<Object> pageList(PageUtil pageUtil, String headStaffUid, String headStaffName, String keyword) {
        if (StringUtils.isBlank(headStaffUid))
            return R.fail("请选择总部员工");

        Page<CmactivationCodeLog> page = cmactivationCodeLogService.pageList(pageUtil, headStaffUid, headStaffName, keyword);
        PageDto<CmactivationCodeLog> pageDto = new PageDto<>();
        pageDto.setTotal(page.getTotalRow());
        pageDto.setList(page.getRecords());
        return R.ok(pageDto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public R<Object> add(String headStaffUid, String headStaffName, String corpUid, String storeUid, String activationTypeUid, Long codeNum) {
        if (StringUtils.isBlank(headStaffUid))
            return R.fail("请选择总部员工");
        if (StringUtils.isBlank(corpUid) || StringUtils.isBlank(storeUid))
            return R.fail("门店信息缺少");
        if (StringUtils.isBlank(activationTypeUid))
            return R.fail("请选择激活码分类");
        if (codeNum == null || codeNum <= 0)
            return R.fail("请输入正确的激活码数量");
        Stores store = storesService.getByCorpUidAndStoreUid(corpUid, storeUid);
        if (store == null)
            return R.fail("门店信息不存在");
        if (!store.getEnabled().equals(1))
            return R.fail("门店已被禁用");

        CmactivationCodeType codeType = cmactivationCodeTypeService.getByUid(activationTypeUid);
        if (codeType == null)
            return R.fail("激活码分类不存在");

        ArrayList<CmactivationCode> cmactivationCodeList = new ArrayList<>();
        for (int i = 0; i < codeNum; i++) {
            CmactivationCode cmactivationCode = AdminEntityUtils.addCmactivationCode(
                    headStaffUid, headStaffName, activationTypeUid, codeType.getActivationName(),
                    codeType.getAmount(), corpUid, storeUid, store.getStoreName()
            );
            cmactivationCodeList.add(cmactivationCode);
        }

        CmactivationCodeLog cmactivationCodeLog =AdminEntityUtils.addCmactivationCodeLog(
                headStaffUid, headStaffName, activationTypeUid, codeType.getActivationName(),
                codeNum, corpUid, storeUid, store.getStoreName()
        );

        boolean b = cmactivationCodeService.saveBatch(cmactivationCodeList);
        if (!b){
            throw new RuntimeException("激活码分配保存失败");
        }
        boolean save = cmactivationCodeLogService.save(cmactivationCodeLog);
        if (!save){
            throw new RuntimeException("激活码日志保存失败");
        }
        return R.ok("激活码分配成功");
    }
}
